﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class KhachHangDAO
    {
        public string LayMaDHLonNhat()
        {
            string sql = "SELECT TOP  1 MaKH FROM KHACHHANG ORDER BY MaKH DESC";
            SqlConnection conn = new SqlConnection();
            conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader reader = cmd.ExecuteReader();
            reader.Read();
            string MaKH = reader.GetString(0);
            conn.Close();
            return MaKH;
        }

        public string NextID(string lastID, string prefixID)
        {
            if (lastID == " ")
            {
                return prefixID + "001";  // fixwidth default
            }
            int nextID = int.Parse(lastID.Remove(0, prefixID.Length)) + 1;
            int lengthNumerID = lastID.Length - prefixID.Length;
            string zeroNumber = "";
            for (int i = 1; i <= lengthNumerID; i++)
            {
                if (nextID < Math.Pow(10, i))
                {
                    for (int j = 1; j <= lengthNumerID - i; i++)
                    {
                        zeroNumber += "0";
                    }
                    return prefixID + zeroNumber + nextID.ToString();
                }
            }
            return prefixID + nextID;

        }
        public string TaoMaKHMoi()
        {
            string LastID = LayMaDHLonNhat();
            string NewID = NextID(LastID, "KH");
            return NewID;
        }

        public int ThemKhachHang(KhachHangDTO khdto)
        {
            int res;
            SqlConnection conn = new SqlConnection();
            conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand("proc_ThemKhachHang", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter sqlPar;

            sqlPar = new SqlParameter("@makh", SqlDbType.VarChar, 6);
            sqlPar.Direction = ParameterDirection.Input;
            sqlPar.Value = khdto.MaKH;
            cmd.Parameters.Add(sqlPar);

            sqlPar = new SqlParameter("@tenkh", SqlDbType.NVarChar, 50);
            sqlPar.Direction = ParameterDirection.Input;
            sqlPar.Value = khdto.TenKH;
            cmd.Parameters.Add(sqlPar);

            sqlPar = new SqlParameter("@dthoai", SqlDbType.Int);
            sqlPar.Direction = ParameterDirection.Input;
            sqlPar.Value = khdto.DThoai;
            cmd.Parameters.Add(sqlPar);

            sqlPar = new SqlParameter("@dchi", SqlDbType.NVarChar, 50);
            sqlPar.Direction = ParameterDirection.Input;
            sqlPar.Value = khdto.Diachi;
            cmd.Parameters.Add(sqlPar);

            sqlPar = new SqlParameter("@cty", SqlDbType.NVarChar, 50);
            sqlPar.Direction = ParameterDirection.Input;
            sqlPar.Value = khdto.Cty;
            cmd.Parameters.Add(sqlPar);

            sqlPar = new SqlParameter("@email", SqlDbType.NVarChar, 50);
            sqlPar.Direction = ParameterDirection.Input;
            sqlPar.Value = khdto.Email;
            cmd.Parameters.Add(sqlPar);

            sqlPar = new SqlParameter("@res", SqlDbType.Int);
            sqlPar.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(sqlPar);

            res = cmd.ExecuteNonQuery();
            res = int.Parse(cmd.Parameters["@res"].Value.ToString());
            conn.Close();
            return res;
        }

        public int CapNhatKhachHang(KhachHangDTO khdto)
        {
            int res;
            SqlConnection conn = new SqlConnection();
            conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand("proc_CapNhatKhachHang", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter sqlPar;

            sqlPar = new SqlParameter("@makh", SqlDbType.VarChar, 6);
            sqlPar.Direction = ParameterDirection.Input;
            sqlPar.Value = khdto.MaKH;
            cmd.Parameters.Add(sqlPar);

            sqlPar = new SqlParameter("@tenkh", SqlDbType.NVarChar, 50);
            sqlPar.Direction = ParameterDirection.Input;
            sqlPar.Value = khdto.TenKH;
            cmd.Parameters.Add(sqlPar);

            sqlPar = new SqlParameter("@dthoai", SqlDbType.Int);
            sqlPar.Direction = ParameterDirection.Input;
            sqlPar.Value = khdto.DThoai;
            cmd.Parameters.Add(sqlPar);

            sqlPar = new SqlParameter("@dchi", SqlDbType.NVarChar, 50);
            sqlPar.Direction = ParameterDirection.Input;
            sqlPar.Value = khdto.Diachi;
            cmd.Parameters.Add(sqlPar);

            sqlPar = new SqlParameter("@cty", SqlDbType.NVarChar, 50);
            sqlPar.Direction = ParameterDirection.Input;
            sqlPar.Value = khdto.Cty;
            cmd.Parameters.Add(sqlPar);

            sqlPar = new SqlParameter("@email", SqlDbType.NVarChar, 50);
            sqlPar.Direction = ParameterDirection.Input;
            sqlPar.Value = khdto.Email;
            cmd.Parameters.Add(sqlPar);

            sqlPar = new SqlParameter("@res", SqlDbType.Int);
            sqlPar.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(sqlPar);

            res = cmd.ExecuteNonQuery();
            res = int.Parse(cmd.Parameters["@res"].Value.ToString());
            conn.Close();
            return res;
        }

        public List<string> LayMaKH()
        {
            List<string> lis= new List<string>();
            string sql = "SELECT MaKH FROM KHACHHANG";
            SqlConnection conn = new SqlConnection();
            conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                lis.Add(reader.GetString(0));
            }
            conn.Close();
            return lis;
        }
        public KhachHangDTO ThongTinKhachHang(string MaKH)
        {
            KhachHangDTO kh= new KhachHangDTO();
            string sql = "SELECT * FROM KHACHHANG WHERE MaKH='"+MaKH+"'";
            SqlConnection conn = new SqlConnection();
            conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                kh.MaKH = reader.GetString(0);
                kh.TenKH = reader.GetString(1);
                kh.DThoai = reader.GetInt32(2);
                kh.Diachi = reader.GetString(3);
               // kh.Cty = reader.GetString(4);
                //kh.Email = reader.GetString(5);
            }
            conn.Close();
            return kh;
        }
    }
}
